package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.WmsWarehouse;
import com.jy.wms.service.WmsWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/*import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;*/

/**
 * @ClassName: WmsWarehouseController
 * @Description: 主表仓库信息表
 * @Author yjs
 * @Date Tue Aug 29 15:39:25 GMT+08:00 2017
 */
@Controller
@RequestMapping("/wmswarehouse")
@Api(value = "/wmswarehouse", description = "关于仓库管理操作")
public class  WmsWarehouseController extends BaseController<WmsWarehouseService,WmsWarehouse> {
    // 将汉字转换为全拼
    /*public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else
                    t4 += Character.toString(t1[i]);
            }
            // System.out.println(t4);
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    // 返回中文的首字母
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

    // 将字符串转移为ASCII码
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }*/
    @ApiOperation(value = "仓库管理 POST请求查询仓库信息",response = JSONObject.class, notes = "POST请求查询仓库信息")
    @PostMapping(value="/list")
    public JSONObject getWmsWarehouse(@RequestBody WmsWarehouse entity) {
        Page<WmsWarehouse> list= new Page<WmsWarehouse>();
        JSONObject json = getJSONObject();
        try {
            list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        }catch(Exception e){
            json = SetStatus.setFunctionType(1,"error","仓库信息",e.getMessage(),"");
        }
        return json;
    }
    /**
     * 查询角色
     * @return
     */
    /*@PostMapping(value = "/addSearchJs")
    @ApiOperation(value = "仓库信息 角色查询",response = JSONObject.class)
    public @ResponseBody
    JSONObject addSearchJs() {
        JSONObject json = getJSONObject();
        try {
            SysRole sysRole = baseService.addSearchJs("仓库管理员",getSessionDB());
            json.put("dictionaryid",sysRole.getId());
            json.put("value",sysRole.getMc());
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }*/
    @PostMapping(value = "/add")
    @ApiOperation(value = "仓库信息 新增仓库信息",response = JSONObject.class, notes = "POST新增仓库信息")
    public JSONObject addWmsWarehouse(@RequestBody WmsWarehouse entity) {
        JSONObject json = getJSONObject();
        try {

            entity.setId(UUIDUtils.uuid());
            /*Properties prop =  new  Properties();
            InputStream inss =WmsWarehouseController.class.getResourceAsStream("/createBean.properties");

            prop.load(inss);
            String url_file= PropertyUtil.getProperty("yjs.url_file");
            String user=PropertyUtil.getProperty("yjs.user");
            String password=PropertyUtil.getProperty("yjs.password");
            String name=PropertyUtil.getProperty("yjs.name");*/

            WmsWarehouse entitys = baseService.searchADD(entity);//数据库去重
            if (entitys != null) {
                json = SetStatus.setFunctionType(1, "warning", "仓库信息", "新增仓库信息重复", "");
            }/* else {
                SysUser sysUser = baseService.searchADDUser(entity, name);//用户查询去重
                if (sysUser != null) {
                    json = SetStatus.setFunctionType(1, "warning", "仓库信息", "新增用户名称重复", "");
                } */else {
                    /*//链接数据库
                    Connection conn = (Connection) DriverManager.getConnection(url_file + name, user, password);
                    //创建数据库
                    String random = getPingYin(entity.getMc());
                    Statement stat = conn.createStatement();
                    stat.executeUpdate("create database " + random);
                    stat.close();
                    conn.close();
                    //链接新数据库
                    Connection conn2 = (Connection) DriverManager.getConnection(url_file + random, user, password);
                    ScriptRunner runner = new ScriptRunner(conn2);
                    //  URL url = this.getClass().getResource("/../sql/k_ckmb.sql");
                    *//*InputStream in = this.getClass().getResourceAsStream("/../sql/k_ckmb.sql");*//*

                    InputStream in = this.getClass().getResourceAsStream("/../sql/zk.sql");
                    InputStream in2 = this.getClass().getResourceAsStream("/../sql/hs.sql");
                    runner.runScript(new InputStreamReader(in, "UTF-8"));
                    runner.setDelimiter("#");
                    runner.runScript(new InputStreamReader(in2, "UTF-8"));
                    runner.closeConnection();

                    conn2.close();*/
                baseService.baseInsert(entity);//添加主库数据库信息
                json = SetStatus.setFunctionType(1, "success", "仓库信息", "新增仓库信息成功", JSONObject.fromObject(entity).toString());
            }

        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1, "warning", "仓库信息", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","仓库信息","新增仓库信息失败","");
        }
        return json;
    }
    @GetMapping(value = "/getWmsWarehouse/{id}")
    @ApiOperation(value = "仓库信息 查看单个仓库信息",response = JSONObject.class, notes = "GET查看单个仓库信息")
    public JSONObject getWmsWarehouseById(@PathVariable("id") String id) {
        JSONObject json = getJSONObject();
        WmsWarehouse entity= null;
        try {
            /*String mc="仓库管理员";*/
            entity = baseService.baseSelectByPrimaryKey(id);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            /*logger.error(e.getMessage());*/
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","仓库信息","查看仓库信息失败","");
        }
        return json;
    }
    @PostMapping(value = "/edit")
    @ApiOperation(value = "仓库信息 修改仓库信息",response = JSONObject.class, notes = "POST修改仓库信息")
    public JSONObject editWmsWarehouse(@RequestBody WmsWarehouse entity){
        JSONObject json = getJSONObject();
        try {
            /*Properties prop = new Properties();
            InputStream inss = WmsWarehouseController.class.getResourceAsStream("/createBean.properties");
            prop.load(inss);
            String url_file = PropertyUtil.getProperty("yjs.url_file");
            String user = PropertyUtil.getProperty("yjs.user");
            String password = PropertyUtil.getProperty("yjs.password");
            String name = PropertyUtil.getProperty("yjs.name");*/
            baseService.baseUpdateByPrimaryKeySelective(entity);//主库数据库更改操作
            json = SetStatus.setFunctionType(1, "success", "仓库信息", "编辑仓库信息成功", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1, "warning", "仓库信息", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","仓库信息","修改仓库信息失败","");
        }
        return json;
    }
}
